﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.Core.Authentication;
using MP.Core.Config;
using MP.Core.Library;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

public partial class Member_Control_AddRoleModules : MP.Core.Authentication.Control.AuthenBase
{
    public string RoleName
    {
        get { return Request["r"]; }
    }

    public string Module
    {
        get { return Request["m"]; }
    }

    public DataTable tblDepart;

    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.MANAGERROLES);

        if (IsAdd() && IsEdit() && IsDelete())
        {
            if (!IsPostBack)
            {
                loadDepart();
                loadCombo();
                LoadData();
            }
        }
        else
        {
            this.Visible = false;
        }
    }

    void LoadData()
    {
        DataTable table = MPRoleProvider.CreateInstant().GetSettingRole(RoleName, Module);
        if (table != null && table.Rows.Count > 0)
        {
            table.DefaultView.Sort = "ModName";
            repSetRole.DataSource = table.DefaultView;
            repSetRole.DataBind();
        }
    }

    void loadCombo()
    {
        DataTable dst = MbModulesManager.CreateInstant().SelectByParentCodeRDT(null);
        ddlModules.DataTextField = "ModName";
        ddlModules.DataValueField = "ModCode";
        ddlModules.DataSource = dst;
        ddlModules.DataBind();

        ddlModules.SelectedValue = Module;
    }

    void loadDepart()
    {
        DataSet dst = MaDepartmentManager.CreateInstant().GetAllDepart();
        if (dst != null && dst.Tables.Count > 0)
            tblDepart = dst.Tables[0];
    }

    public string getImg(bool f)
    {
        string img = "";

        if (f)
            img = "checked.gif";
        else
            img = "unchecked.gif";

        return img;
    }

    protected void repSetRole_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "del":
                MPRoleProvider.CreateInstant().DeleteRoleAction(new Guid(e.CommandArgument.ToString()));
                Response.Redirect(Request.RawUrl);
                break;
            default:
                string[] arg = e.CommandArgument.ToString().Split('|');
                string roleName = arg[0];
                string modCode = arg[1];

                MPRoleProvider.CreateInstant().EditActionRole(roleName, modCode, e.CommandName, Convert.ToBoolean(arg[2]));
                Response.Redirect(Request.RawUrl);
                break;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (repSetRole.Items.Count > 0)
        {
            for (int i = 0; i < repSetRole.Items.Count; i++)
            {
                string ModCode = ((HiddenField)repSetRole.Items[i].FindControl("hidModCode")).Value;
                Guid RoleId = new Guid(((HiddenField)repSetRole.Items[i].FindControl("hidRoleId")).Value);
                Guid DepartId = new Guid(((DropDownList)repSetRole.Items[i].FindControl("ddlDepart")).SelectedValue);

                bool IsShow = ((CheckBox)repSetRole.Items[i].FindControl("chkShow")).Checked;
                bool IsShow2 = ((CheckBox)repSetRole.Items[i].FindControl("chkShowToUser")).Checked;
                bool IsAdd = ((CheckBox)repSetRole.Items[i].FindControl("chkAdd")).Checked;
                bool IsEdit = ((CheckBox)repSetRole.Items[i].FindControl("chkEdit")).Checked;
                bool IsEdit2 = ((CheckBox)repSetRole.Items[i].FindControl("chkEditToUser")).Checked;
                bool IsDelete = ((CheckBox)repSetRole.Items[i].FindControl("chkDelete")).Checked;
                bool IsDelete2 = ((CheckBox)repSetRole.Items[i].FindControl("chkDeleteToUser")).Checked;
                bool IsEnable = ((CheckBox)repSetRole.Items[i].FindControl("chkEnable")).Checked;
                bool IsAll = ((CheckBox)repSetRole.Items[i].FindControl("chkIsAll")).Checked;

                MPRoleProvider.CreateInstant().UpdateRoleModule(ModCode, RoleId, DepartId, IsShow, IsShow2, IsAdd, IsEdit, IsEdit2, IsDelete, IsDelete2, IsEnable, IsAll);
            }

            Response.Redirect("/Member/RoleModules.aspx?r=" + Server.UrlEncode(RoleName) +
                "&m=" + Server.UrlEncode(Module));
        }
    }

    protected void repSetRole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlDepart");
        ddl.SelectedValue = row["DepartId"].ToString();
    }
}
